package com.cxs.serviceabilitydemo.slice;

import com.cxs.serviceabilitydemo.LocalServiceAbility;
import com.cxs.serviceabilitydemo.ResourceTable;
import com.cxs.serviceabilitydemo.connections.MyRemoteProxy;
import com.cxs.serviceabilitydemo.util.DeviceUtils;
import com.cxs.serviceabilitydemo.util.UIUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.DirectionalLayout;
import ohos.bundle.ElementName;
import ohos.rpc.IRemoteObject;

public class MainAbilitySlice extends AbilitySlice {
    private MyRemoteProxy myRemoteProxy = null;

    private IAbilityConnection conn = new IAbilityConnection() {
        // 连接成功时会调用此方法
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) {
            // iRemoteObject就是RemoteServiceAbility类里onConnect方法返回的remote
            myRemoteProxy = new MyRemoteProxy(iRemoteObject);
        }

        // 当连接奔溃或出现异常断开时回调此方法
        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int i) {

        }
    };

    Intent localIntent = null, remoteIntent = null;
    String deviceId;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        deviceId = DeviceUtils.getDeviceId();
        localIntent = getIntent("", "LocalServiceAbility");
        remoteIntent = getIntent(deviceId, "RemoteServiceAbility");

        initView();
    }

    void initView() {
        // 启动本地Service
        findComponentById(ResourceTable.Id_btn_start_local_service)
                .setClickedListener(c -> {
                    System.out.println("start Local Service");
                    startAbility(localIntent);
                });
        // 关闭本地Service
        findComponentById(ResourceTable.Id_btn_stop_local_service)
                .setClickedListener(c -> {
                    stopAbility(localIntent);
                });

        // 启动远程Service
        findComponentById(ResourceTable.Id_btn_start_remote_service)
                .setClickedListener(c -> {
                    startAbility(remoteIntent);
                });

        // 关闭远程Service
        findComponentById(ResourceTable.Id_btn_stop_remote_service)
                .setClickedListener(c -> {
                    stopAbility(remoteIntent);
                });

        // 连接远程Service
        findComponentById(ResourceTable.Id_btn_connect_remote_service)
                .setClickedListener(c -> {
                    connectAbility(remoteIntent, conn);
                });

        // 连接后调用远程Service的方法
        findComponentById(ResourceTable.Id_btn_run_remote_service)
                .setClickedListener(c -> {
                    if (myRemoteProxy != null) {
                        int plus = myRemoteProxy.plus(188, 567);
                        System.out.println(myRemoteProxy);
                        UIUtils.showTip(this, "plus: " + plus);
                    }
                });
        // 断开远程Service
        findComponentById(ResourceTable.Id_btn_disconnect_remote_service)
                .setClickedListener(c -> {
                    disconnectAbility(conn);
                });
    }

    Intent getIntent(String deviceId, String abilityName) {
        Intent intent = new Intent();
        if (deviceId == "") {
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId(deviceId)
                    .withBundleName("com.cxs.serviceabilitydemo")
                    .withAbilityName(abilityName)
                    .build();
            intent.setOperation(operation);
        } else {
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId(deviceId)
                    .withBundleName("com.cxs.serviceabilitydemo")
                    .withAbilityName(abilityName)
                    .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                    .build();
            intent.setOperation(operation);
        }
        return intent;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
